package com.suning.sawp.service.impl.customer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.customer.PtyMemberRecordDto;
import com.suning.sawp.dto.customer.PtyMemberRecordResponse;
import com.suning.sawp.dto.customer.PtyMemberRecordVo;
import com.suning.sawp.service.util.AESUtils;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.service.util.DateUtils;

/**
 * 企业会员记录服务
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("ptyMemberRecordService")
public class PtyMemberRecordServiceImpl {

    private static final Logger LOGGER = LoggerFactory.getLogger(PtyMemberRecordServiceImpl.class);
    
    @Resource
    DalClient dalClient;
    
    /**
     * 查询企业会员记录列表
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param currIndex
     * @param pageSize
     * @param month
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public ReturnMsg<PtyMemberRecordResponse> queryPtyMemberRecords(String staffId, int currIndex, int pageSize, 
            Date month) {
        LOGGER.info("queryPtyMemberRecords enter, staffId={}", staffId);
        PtyMemberRecordResponse resp = new PtyMemberRecordResponse();
        ReturnMsg<PtyMemberRecordResponse> returnMsg = new ReturnMsg<PtyMemberRecordResponse>();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("currIndex", currIndex);
        paramMap.put("pageSize", pageSize);
        // 筛选条件月份不为空时，取月份的开始时间,结束时间
        if (null != month) {
            paramMap.put("monthBegin", DateUtils.getFirstDateOfMonth(month));
            paramMap.put("monthEnd", DateUtils.getLastDateOfMonth(month));
        } else {
            // 默认取当前00:00:00
            paramMap.put("yesterday", DateUtils.getNowZeroPoint());
            // 取昨天的时间
            resp.setEndTime(DateUtils.getNextShortStr(-1));
        }
        LOGGER.debug("queryPtyMemberRecords paramMap={}", paramMap);
        // 查询总记录数
        Integer total = dalClient.queryForObject("outGrow.QUERY_PTY_MEMBER_TOTAL", paramMap, Integer.class);
        LOGGER.debug("queryPtyMemberRecords total={}", total);
        if (null != total && total.intValue() > 0) {
            // 设置总记录数
            resp.setTotal(total.intValue());
            List<PtyMemberRecordDto> list = dalClient.queryForList("outGrow.QUERY_PTY_MEMBER_LIST", paramMap, PtyMemberRecordDto.class);
            LOGGER.debug("queryPtyMemberRecords query list={}", list);
            // 转换数据对象
            resp.setList(convertMemberRecord(list));
        } else {
            // 转换数据对象
            resp.setList(new ArrayList<PtyMemberRecordVo>());
        }
        returnMsg.setData(resp);
        LOGGER.info("queryPtyMemberRecords exit, staffId={}", staffId);
        return returnMsg;
    }
    
    /**
     * 转换记录对象
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param list
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<PtyMemberRecordVo> convertMemberRecord(List<PtyMemberRecordDto> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            List<PtyMemberRecordVo> resp = new ArrayList<PtyMemberRecordVo>();
            PtyMemberRecordVo record;
            for (PtyMemberRecordDto dto : list) {
                record = new PtyMemberRecordVo();
                record.setId(dto.getId());
                // 脱敏客户名称
                record.setCustName(DJStringUtils.desensitiseName(dto.getCustName()));
                // 加密手机号
                record.setMobile(AESUtils.encrypt(dto.getMobile(), ConfigConstants.AES_KEY));
                record.setOraLongName(dto.getOraLongName());
                record.setRegDate(null == dto.getRegDate() ? "" : 
                    DateUtils.getDateStrByFormat(dto.getRegDate(), "yyyy-MM-dd HH:mm"));
                resp.add(record);
            }
            return resp;
        }
        return Collections.emptyList();
    }
}
